<!--This jsp file is called in case the courses link was pressed.
	It receives from the sql database the courses and shows them-->
<%@page import="sql.DBManagment"%>
<%@page import="sql.record.*"%>
<%@page import="java.util.*"%>
<%	
	User loginUser = new User();
	loginUser.userName = (String) session.getAttribute("loggedUser");
	User storedUser = new User();
	try  {
		List<User> userList = DBManagment.getRecord(loginUser);
		storedUser = userList.get(0);
	}catch(Exception e){
		%>
		<script type = "text/javascript" src = "script/end_session.js"></script>		
		<%
	}
%>
<%
List<TableRecord> courseSchedList = DBManagment.getMyCourses((String) session.getAttribute("loggedUser"));

//will get all courses
List<Course> courses = new LinkedList<Course>();
for(TableRecord t: courseSchedList){
	if(t instanceof Course){
		int lastIndex = courses.size() - 1;
		Course currentCourse = (Course)t;
		if((lastIndex == -1) || (!courses.get(lastIndex).courseName.equals(currentCourse.courseName))){
			courses.add((Course)t);
		}
	}
}
%>	
<script type = "text/javascript" src = "script/crsForms.js"></script>
<div class="bodytext" style="padding:12px;" align="justify">
  	<form name="crsForm" id="crsForm" class="tableForm">
		<!-- on a click, actions from crsForm.js will be activated -->
	    <strong>My Courses</strong>
		<div id="coursesControlPanel">

		    <input type="button" id="markAll" value="Mark All" />
		    <input type="button" id="UnMarkAll" value="Unmark All" />
		    <input type="button" id="unregisterMarked" value="Unregister Marked" class = "handleMarked"/>
		    <input type="button" value="Add Courses" id="add_courses" 
		    	   				 title="Add Courses" class="menuLink" />
		</div>
	  	<div id = "container">
		    <ul class = "grid">
		    	<%
		    		for(Course c:courses){
		    	%>
			      <li>
			      	<%=c.courseName %>
			      	<input type="checkbox" name="mark" id="mark" value=<%="\"" + c.courseName + "\"" %> /><br/><br/>
					<b>Course Name: </b><%=c.courseName%><br/>
					<b>Credit Points: </b><%=c.creditPoints%><br/>
					<b>Capacity: </b><%=c.capacity%></br>
					<b>Description: </b><br/><%=c.description%><br/><br/>
					<strong>Schedule</strong><br/>
					<%
						String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday"};
						for(TableRecord t : courseSchedList){
							if((t instanceof CourseSchedule) && (((CourseSchedule)t).courseName.equals(c.courseName))){
							CourseSchedule currentSched = ((CourseSchedule)t);
						%>
							<%
								String startTime = currentSched.startHour.toString();
								String endTime = currentSched.endHour.toString();
								if(currentSched.startHour.intValue() < 1000){
									startTime = "0" + startTime;
								}
								if(currentSched.endHour.intValue() < 1000){
									endTime = "0" + endTime;
								}							
							%>
							<%=days[currentSched.day]%>, <%=startTime%> to <%=endTime%> <br/>
					<%		
						}
					}
				%>     	
			      	
			      </li>
			    <%
		    		}
			    %>
		    </ul>
		</div>
		<br/>		  
  </form>
</div>